import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layouts/PageLayout.vue'
import UserLogin from '@/views/UserLogin.vue'

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/UserLogin',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/HomePage.vue'),
        meta: { title: '控制台' },
      },
      {
        path: 'ScreenPreview',
        component: () => import('@/views/screen/ScreenPreview.vue'),
        meta: { title: '画面预览' },
      },
      {
        path: 'ScreenEditing',
        component: () => import('@/views/screen/ScreenEditing.vue'),
        meta: { title: '画面编辑' },
      },
       {
        path: 'ProjectList',
        component: () => import('@/views/screen/ProjectList.vue'),
        meta: { title: '项目列表' },
      },
      {
        path: 'picture',
        component: () => import('@/views/screen/PictureFile.vue'),
        meta: { title: '画面文件' },
      },
      {
        path: 'drive',
        component: () => import('@/views/device/DriverList.vue'),
        meta: { title: '驱动列表' },
      },
      {
        path: 'device',
        component: () => import('@/views/device/CommunicationEquipment.vue'),
        meta: { title: '通讯设备' },
      },
      {
        path: 'deviceVariable',
        component: () => import('@/views/device/CommunicationVariables.vue'),
        meta: { title: '通讯变量' },
      },
      {
        path: 'alarmStrategy',
        component: () => import('@/views/alarms/AlarmCondition.vue'),
        meta: { title: '报警条件' },
      },
      {
        path: 'alarmData',
        component: () => import('@/views/alarms/AlarmData.vue'),
        meta: { title: '报警数据' },
      },
      {
        path: 'historyStorageStrategy',
        component: () => import('@/views/storage/StorageRequirements.vue'),
        meta: { title: '存储条件' },
      },
      {
        path: 'historyData',
        component: () => import('@/views/storage/HistoricalData.vue'),
        meta: { title: '历史数据' },
      },
      {
        path: 'computeVariable',
        component: () => import('@/views/compute/CalculateVariables.vue'),
        meta: { title: '计算变量' },
      },
      {
        path: 'executeWhere',
        component: () => import('@/views/compute/ExecutionConditions.vue'),
        meta: { title: '执行条件' },
      },
      {
        path: 'holdingVariable',
        component: () => import('@/views/compute/MaintainVariables.vue'),
        meta: { title: '保持变量' },
      },
      {
        path: 'computeScript',
        component: () => import('@/views/compute/ScriptCalculation.vue'),
        meta: { title: '脚本计算' },
      },
      {
        path: 'httpConfig',
        component: () => import('@/views/transmission/HttpForward.vue'),
        meta: { title: 'http转发' },
      },
      {
        path: 'mqttConfig',
        component: () => import('@/views/transmission/MqttForward.vue'),
        meta: { title: 'mqtt转发' },
      },
      {
        path: 'modbusTcpService',
        component: () => import('@/views/transmission/ModbusForward.vue'),
        meta: { title: 'modbus转发' },
      },
       {
        path: 'log',
        component: () => import('@/views/permission/OperationDiary.vue'),
        meta: { title: '操作日记' },
      },
      {
        path: 'role',
        component: () => import('@/views/permission/RoleManagement.vue'),
        meta: { title: '角色管理' },
      },
      {
        path: 'menu',
        component: () => import('@/views/permission/MenuManagement.vue'),
        meta: { title: '菜单管理' },
      },
      {
        path: 'user',
        component: () => import('@/views/permission/UserManagement.vue'),
        meta: { title: '用户管理' },
      },
    ],
  },
  { path: '/UserLogin', component: UserLogin },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 路由守卫：检查是否已登录
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token') // 检查本地存储是否有 token
  if (to.path !== '/UserLogin' && !isAuthenticated) {
    next('/UserLogin')
  } else {
    next()
  }
})

export default router
